<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class VarietiesController extends Controller
{
    //
    //品种首页 显示内容
    public function getIndex(Request $request)
    {

        $count = 1;
        //判断是否有搜索
        if($request -> input('keywords')){
            $res = DB::table('varieties')
                ->where('name','like','%'.$request->input('keywords').'%')
                ->orWhere('properties','like','%'.$request->input('keywords').'%')
                ->paginate(4);
        }else{
            //获取所有的信息
            $res = DB::table('varieties') -> paginate(4);    
        }
        //获取所有的信息
        $data = $request -> all();
    	return view('Admin.varieties.index',['varieties' => $res,'count' => $count,'data' => $data]);
    }

    //品种添加页
    public function  getAdd()
    {
        //首字母
        $first = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
        // 产新时间
        $time = array('一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月');
        //属性数组
        $sx = array('易掺假','易储存','易走色变油','易虫易霉','易受疫情影响','易受倒春寒影响','易受旱情影响','易受雨涝影响','生长期3年以上','生长期1-3年','生长期1年','生长期易受行情影响','进口','毒性','贵细','种植费工时','采挖加工费工时','常用大宗','基药包含','小量冷背','野生主供','家种主供','香料类','滋补类','汤料类','茶饮类 ','酒料类');
        //产区数组
        $area = array('北京市','天津市','河北省','山西省','内蒙古自治区','辽宁省','吉林省','黑龙江省','上海市','江苏省','浙江省','安徽省','福建省','江西省','山东省','河南省','湖北省','湖南省','广东省','广西壮族自治区','海南省','重庆市','四川省','贵州省','云南省','西藏自治区','陕西省','甘肃省','青海省');



    	return view('Admin.varieties.add',['first' => $first,'sx' => $sx,'time' => $time,'area' => $area]);
    }

    //执行插入
    public function postInsert(Request $request)
    {

        $data = $request -> except(['_token','image','properties','time','area']);
        //处理药材属性
        $data['properties'] = trim($request -> input('properties'),',');
        $data['area'] = trim($request -> input('area'),',');
        $time = ($request -> input('time'));
        $data['time'] = '';
        foreach ($time as $key => $value) {
            $data['time'] .= $value . ',';
        }
        $data['time'] = trim($data['time'],',');
        // dd($data);
        // dd($this -> upload($request,'image'));
        $data['image'] = $this -> upload($request,'image');
    	// dd($data);
    	//执行插入
    	$res = DB::table('varieties') -> insert($data);
    	if($res){
    		return redirect('/admin/varieties') -> with('success','品种插入成功');
    	}else{
    		return back() -> with('error','品种插入失败');
    	}
    }

    //品种修改页
    public function getEdit($id)
    {
    	//获取该条信息
    	$res = DB::table('varieties') -> where('id',$id) -> first();
        //首字母
        $first = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
        // 产新时间
        $time = array('一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月');
        //属性数组
        $sx = array('易掺假','易储存','易走色变油','易虫易霉','易受疫情影响','易受倒春寒影响','易受旱情影响','易受雨涝影响','生长期3年以上','生长期1-3年','生长期1年','生长期易受行情影响','进口','毒性','贵细','种植费工时','采挖加工费工时','常用大宗','基药包含','小量冷背','野生主供','家种主供','香料类','滋补类','汤料类','茶饮类 ','酒料类');
        //产区数组
        $area = array('北京市','天津市','河北省','山西省','内蒙古自治区','辽宁省','吉林省','黑龙江省','上海市','江苏省','浙江省','安徽省','福建省','江西省','山东省','河南省','湖北省','湖南省','广东省','广西壮族自治区','海南省','重庆市','四川省','贵州省','云南省','西藏自治区','陕西省','甘肃省','青海省');

    	// dd($res);
    	return view('Admin.varieties.edit',['varieties' => $res,'first' => $first,'sx' => $sx,'time' => $time,'area' => $area]);
    }

    //执行修改
    public function postUpdate(Request $request)
    {
    	$data = $request -> except(['_token','id','image']);
        $data['image'] = $this -> upload($request,'image');
        // dd($data['image']);
        //如果上传的图片为空,则设置默认值
        if($data['image'] == ''){
            $data['image'] = '/bootcss/admin/img/varieties/varieties_default.jpg';
        }
    	$id = $request -> input('id');
    	// dd($id);
    	// dd($data);
        //获取原信息
        $old = DB::table('varieties') -> where('id',$id) -> first() -> image;
        // dd($old);
    	//执行修改
    	$res = DB::table('varieties') -> where('id',$id) -> update($data);
    	// dd($res);
        if($res){
            if(!$old == '/bootcss/admin/img/varieties/varieties_default.jpg'){
                unlink('.' . $old -> image);
            }   
    		return redirect('/admin/varieties') -> with('success','信息修改成功');
    	}else{
    		return back() -> with('error','信息修改失败');
    	}
    }

    //品种图片上传的处理方法
    public function upload($request,$name)
    {
        // dd($request -> all());
        
        if($request -> hasFile($name)){
            //随机图片名
            $n = md5(rand(1000,9999).time());
            //获取上传的后缀名
            $suffix = $request -> file($name) -> getClientOriginalExtension();
            //限制上传文件的后缀名类型
            $arr = array('jpg','png','jpeg','gif');
            //判断
            if(!in_array($suffix,$arr)){
                die('上传文件后缀名不符合要求');
            }
            //移动文件
            $request -> file($name) -> move('./bootcss/admin/img/varieties/' , $n . '.' . $suffix);
            //需要在文件上传成功后返回一个路径,方便上传数据库
            return '/bootcss/admin/img/varieties/' . $n . '.' . $suffix;
        }
        return false;
    }

    //执行删除
    public function getDelete($id)
    {
    	// dd($id);
    	//执行
    	$res = DB::table('varieties') -> where('id',$id) -> delete();
    	if($res){
    		return redirect('/admin/varieties') -> with('success','信息删除成功');
    	}else{
    		return back() -> with('error','信息删除失败');
    	}
    }
}	

